package com.truckmaster.driver.sharesdk;

import android.app.Activity;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import partybuild.xinye.com.sharesdk.R;

/**
 * 分享工具类
 *
 * ShareSDK说明:http://wiki.mob.com/%E5%BF%AB%E6%8D%B7%E5%88%86%E4%BA%AB%E5%8F%8A%E5%85%B6%E8%87%AA%E5%AE%9A%E4%B9%89/#h1-2
 * 其他类型参数表:http://wiki.mob.com/%E4%B8%8D%E5%90%8C%E5%B9%B3%E5%8F%B0%E5%88%86%E4%BA%AB%E5%86%85%E5%AE%B9%E7%9A%84%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E/
 *
 * @author wangheng
 */

public class ShareUtils {
    public static void showShare(@NonNull Activity activity,
                          @NonNull String title,
                          @NonNull String text,
                          @NonNull String url,
                          @Nullable String imageUrl) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle(title);
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(text);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath(imagePath);//确保SDcard下面存在此张图片
        oks.setImageUrl(imageUrl);
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
//        oks.setSite(App.getInstance().getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
//        oks.setSiteUrl("http://sharesdk.cn");
        // 启动分享GUI
        oks.show(activity);
    }


    public static void shareWithQQLink(String title, String text, String imageUrl,String titleUrl,
                                       final PlatformActionListener listener){
        /**
         * title	titleUrl	text	imagePath imageUrl
         */
        Platform.ShareParams params = new Platform.ShareParams();
        params.setTitle(title);
        params.setTitleUrl(titleUrl);
        params.setText(text);
        params.setImageUrl(imageUrl);

        Platform platform = ShareSDK.getPlatform(QQ.NAME);
        setPlatformActionListener(platform,listener);
        // 执行图文分享
        platform.share(params);

    }



    public static void shareWithWechatLink(String title,
                                           String text,
                                           String imageUrl,
                                           String url,
                                           PlatformActionListener listener){
        /**
         * shareType(Platform.SHARE_WEBPAGE)	title	text(朋友圈不显示此字段)	imagePath	 url
         imageUrl
         imageData
         */
        Platform.ShareParams params = new Platform.ShareParams();
        params.setShareType(Platform.SHARE_WEBPAGE);
        params.setTitle(title);
        params.setText(text);
        params.setImageUrl(imageUrl);
        params.setUrl(url);

        Platform platform = ShareSDK.getPlatform(Wechat.NAME);
        setPlatformActionListener(platform,listener);
        // 执行图文分享
        platform.share(params);
    }

    public static void shareWithWechatMomentsLink(String title,
                                                  String text,
                                                  String imageUrl,
                                                  String url,
                                                  PlatformActionListener listener){
        /**
         * shareType(Platform.SHARE_WEBPAGE)	title	text(朋友圈不显示此字段)	imagePath	 url
         imageUrl
         imageData
         */
        Platform.ShareParams params = new Platform.ShareParams();
        params.setShareType(Platform.SHARE_WEBPAGE);
        params.setTitle(title);
        params.setText(text);
        params.setImageUrl(imageUrl);
        params.setUrl(url);

        Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
        setPlatformActionListener(platform,listener);
        // 执行图文分享
        platform.share(params);

    }

    public static void shareWithWechatFile(String title, Bitmap bitmap, String file,
                                           PlatformActionListener listener){
        Platform.ShareParams params = new Platform.ShareParams();
        params.setShareType(Platform.SHARE_FILE);
        params.setTitle(title);
        params.setText(title);
        params.setImageData(bitmap);
        params.setFilePath(file);

        Platform platform = ShareSDK.getPlatform(Wechat.NAME);
        setPlatformActionListener(platform,listener);
        // 执行图文分享
        platform.share(params);

    }

    private static void setPlatformActionListener(Platform platform,
                                                  final PlatformActionListener listener) {
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                if(listener != null){
                    listener.onComplete(platform,i,hashMap);
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                if(listener != null){
                    listener.onError(platform,i,throwable);
                }

            }

            @Override
            public void onCancel(Platform platform, int i) {
                if(listener != null){
                    listener.onCancel(platform,i);
                }
            }
        }); // 设置分享事件回调
    }
}
